// 1. 引入
import { defineStore } from "pinia";
import { computed, ref } from "vue";
import axios from "axios";

const API_URL = "http://geek.itheima.net/v1_0/channels";

export const userCountStore = defineStore("counter", () => {
  // 2. 定义数据
  const count = ref(0);
  // 3. 定义方法
  const add = () => {
    count.value++;
  };

  // getter
  const doubleCount = computed(() => count.value * 2);

  const data = ref([]);
  // 异步
  const getData = async () => {
    const res = await axios.get(API_URL);
    console.log(res);
    data.value = res.data.data.channels;
  };

  // 4. 返回数据和方法
  return {
    count,
    data,
    add,
    doubleCount,
    getData,
  };
});
